<template>
  <div class="header">
    <img src="@/assets/bookLogo.png" alt="" srcset="" />
    <HeaderSearch />
    <HeaderCart />
    <span v-if="!user"
      >你好，请 <router-link to="/login">登录</router-link>免费
      <router-link to="/register">注册</router-link>
    </span>
    <span v-else
      >欢 迎 您，{{ user.username }},
      <a href="javascript:;" @click="logout">退出登录</a>
    </span>
  </div>
</template>

<script>
import HeaderCart from "../Header/HeaderCart.vue";
import HeaderSearch from "../Header/HeaderSearch.vue";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Header",
  components: {
    HeaderCart,
    HeaderSearch,
  },
  computed: {
    // user带有命名空间
    ...mapState("user", ["user"]),
  },
  methods: {
    logout() {
      this.deleteUser();
    },
    ...mapMutations("user", ["deleteUser"]),
  },
};
</script>

<style scoped>
.header {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.header img {
  width: 50px;
  height: 38px;
  padding: 20px 0;
  margin-right: 100px;
  cursor: pointer;
}
.header span {
  margin-left: 20px;
  display: inline-block;
}
.header a {
  text-decoration: none;
  color: rgb(255, 0, 0);
}
</style>
